<?php 
// LOCALE MANAGEMENT
// *********************************************************
	class Locale
	{
		const COOKIE_NAME = 'ICM_mylocale';
	
		static $valid_ids = array();

		// This array maps allowed locale IDs with valid locale IDs.
		static $allowed_ids = array (
			'en_US' => 'en',
			'en' => 'en',
			'es' => 'es',
			'es_ES' => 'es',
			'es_MX' => 'es'
			);

		static $current = NULL;
	
		// LOCALE DETERMINATION
		// -----------------------------------------------------------
	
		static function find() {
			// Try to set locale. First with POST, second with a cookie, third
			// with the first element of the $lenguajes array.
			
			if (array_key_exists(Locale::COOKIE_NAME, $_COOKIE))
			{
				if (is_valid_locale($_COOKIE[Locale::COOKIE_NAME]))
				{
					load_locale($_COOKIE[Locale::COOKIE_NAME]);
					return;
				}
			}
			if (is_allowed_locale($_SERVER['HTTP_ACCEPT_LANGUAGE']))
			{
				load_locale(Locale::$allowed_ids[$_SERVER['HTTP_ACCEPT_LANGUAGE']]);
			}
			else
			{
				load_locale('en');
			}
		}
	
		static function load($loc) {
			include_once(Globals::base_url . $loc . '\lang.php');
			setcookie(Locale::COOKIE_NAME, Locale::$current->id);
			Locale::$current->set_php_locale();
		}

		static function is_valid($locname)
		{
			return array_key_exists($locname, Locale::$valid_ids);
		}

		static function is_allowed($locname)
		{
			return array_key_exists($locname, Locale::$allowed_ids);
		}
	}
	Locale::$valid_ids['es'] = new LocaleInfo('es', 'Spanish_Spain.1252', 'Español');
	Locale::$valid_ids['en'] = new LocaleInfo('en', 'English_United States.1252', 'English');

